﻿using System.Collections.Generic;
using System;
using VelocityDb;

namespace RESTester
{
    public class Request : OptimizedPersistable
    {
        #region 4 db

        public const UInt32 PlaceInDatabase = 10;

        public override bool AllowOtherTypesOnSamePage
        {
            get
            {
                return true;
            }
        }

        public override UInt32 PlacementDatabaseNumber
        {
            get
            {
                return PlaceInDatabase;
            }
        }

        public override bool Equals(object obj)
        {
            bool result = false;

            Request request = obj as Request;
            if (request != null &&
                request.Id.Equals(Id) &&
                request.Url.Equals(Url) &&
                request.Method.Equals(Method) &&
                request.Data.Equals(Data) &&
                request.ContentType.Equals(ContentType) &&
                IsHeadersEquals(request.Headers))
            {
                result = true;
            }

            return result;
        }

        private bool IsHeadersEquals(List<HTTPHeader> list)
        {
            bool result = true;

            if (list.Count == Headers.Count)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (!list[i].Name.Equals(Headers[i].Name) ||
                        !list[i].Value.Equals(Headers[i].Value))
                    {
                        result = false;
                    }
                }
            }

            return result;
        }

        #endregion

        public Request(string url, HTTPMethod method, string data, List<HTTPHeader> headers, string contentType, string name = null)
        {
            _url = url;
            _method = method;
            _data = data;
            _headers = headers;
            _contentType = contentType ?? "application/x-www-form-urlencoded";
            _name = name ?? "Request";
        }
        
        private string _data;
        private List<HTTPHeader> _headers;
        private string _url;
        private HTTPMethod _method;
        private string _name;
        private string _contentType;

        public string Data { get { return _data; } }
        public List<HTTPHeader> Headers { get { return _headers; } }
        public string Url { get { return _url; } }
        public HTTPMethod Method { get { return _method; } }
        public string Name { get { return _name; } }
        public string ContentType { get { return _contentType; } }
        public int Id { get; set; }
    }
}
